const app = getApp()
import {
  wxGetUserProfile,
  fetchGetUserProfile
} from '../../api/login'
const funAList =[
  // {
  //   id: 'func-a-1',
  //   name: '购物车',
  //   icon: 'cart-icon',
  //   event: 'cart'

  // },
  {
    id: 'func-a-2',
    name: '我的信息',
    icon: 'person-icon',
    event: 'editInfo'


  },
  {
    id: 'func-a-3',
    name: '安全中心',
    icon: 'shiefld-icon',
    event: 'shield'
  },
]
Page({
  data: {
    storeName: '',
    accounts: [{
        id: 'account-1',
        name: '余额',
        data: '0.00',
        event: 'balance'
      },
      {
        id: 'account-2',
        name: '积分',
        data: '0',
        event: 'score'

      },
      {
        id: 'account-3',
        name: '卡',
        data: '0',
        event: 'card'

      }, {
        id: 'account-4',
        name: '优惠券/码',
        data: '0',
        event: 'coupon'
      }
    ],
    status: [{
        id: 'status-1',
        name: '待付款',
        icon: 'order-a',
        status: '0'
      },
      {
        id: 'status-2',
        name: '待发货',
        icon: 'order-b',
        status: '1'

      },
      {
        id: 'status-3',
        name: '待收货',
        icon: 'order-c',
        status: '2'

      },
      {
        id: 'status-4',
        name: '已取消',
        icon: 'order-d',
        status: '4'

      },
    ],
    funAList: funAList,
    funBList: [{
      id: 'func-b-1',
      name: '收货地址',
      icon: 'location-icon',
      event: 'address'
    }, ],
    user: {
      name: '',
      avatar: '',
    }
  },
  onShow() {
    this.setData({
      storeName: wx.getStorageSync('shop_name')
    })
    this.initInfo()
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    })
  },
  toPage(path) {
    wx.navigateTo({
      url: path,
    })
  },
  toShopPage(path) {
    wx.navigateToMiniProgram({
      appId: 'wx7b872fc1357a16f8',
      path,
      extraData: {},
      envVersion: "trial",
      success(res) {
        // 打开成功
      }
    })
  },
  excuteAction(e) {
    const {
      event
    } = e.currentTarget.dataset
    console.log(event)
    switch (event) {
      case 'address':
        this.toPage('/pages/address/address')
        break;
      case 'shield':
        this.toPage('/pages/shield/shield')
        break;
      case 'cart':
        wx.switchTab({
          url: '/pages/cart/cart',
        })
        break;
      case 'editInfo':

        this.toPage('/pages/my/editInfo/editInfo')
        break;
      default:
        wx.showToast({
          title: '功能开发中',
          icon: 'none'
        })
        break;

    }
  },
  toOrder(e) {
    let {
      status
    } = e.currentTarget.dataset

    wx.navigateTo({
      url: `/pages/order/order?status=${status||''}`,
    })
  },
  async getUserInfo() {
    // wxGetUserProfile , fetchGetUserProfile
    const user = await wxGetUserProfile()
    console.log(user)
    if (user.userInfo) {
      const {
        nickName,
        avatarUrl
      } = user.userInfo
      fetchGetUserProfile({
          name: nickName,
          avatar: avatarUrl,
        }).then(res => {
          console.log(res)
          const {
            info
          } = res.data
          wx.setStorageSync('avatar', info.avatar)
          wx.setStorageSync('name', info.realName)
          this.initInfo()
        })
        .catch(err => {
          console.log(err)
        })
    } else {
      wx.showToast({
        title: '取消授权',
        icon: 'none'
      })
    }

    // this.setData({
    //   isLogin: true
    // })
  },
  initInfo() {
    const tempUser = {
      balance: 0,
      score: 0,
      card: 0,
      coupon: 0,
    }
    const avatar = wx.getStorageSync('avatar') || ''
    const name = wx.getStorageSync('name') || ''
    const user = wx.getStorageSync('user') || tempUser
    const {
      balance,
      score,
      card,
      coupon
    } = user

    let {
      accounts
    } = this.data
    if (balance) {
      accounts[0].data = balance
    }
    if (score) {
      accounts[1].data = score
    }
    if (card) {
      accounts[2].data = card
    }
    if (coupon) {
      accounts[3].data = coupon
    }
    this.setData({
      user: {
        name,
        avatar,
      },
      accounts,
    })
  },
  excuteTopAction(e) {
    const {
      event
    } = e.currentTarget.dataset
    console.log(event)
    switch (event) {
      case 'balance':
        this.toShopPage('/pages/index/index')
        break;
      case 'score':
        this.toShopPage('/pages/index/index')
        break;
      case 'card':
        this.toShopPage('/pages/index/index')
        break;
      case 'coupon':
        this.toShopPage('/pages/index/index')
        break;
      default:
        wx.showToast({
          title: '功能开发中',
          icon: 'none'
        })
        break;

    }
  },

})